package com.sports.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sports.entity.Admin;
import com.sports.entity.Athletes;
import com.sports.entity.Judge;
import com.sports.service.AdminService;
import com.sports.service.AthletesService;
import com.sports.service.JudgeService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AthletesController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author WGH
 * @Create 2024/3/26 21:16
 * @Version 1.0
 */
@RestController
@RequestMapping("athletes")
public class AthletesController {

    @Autowired
    AthletesService athletesService;

    /**
     * 添加运动员
     *
     * @param password1
     * @param athletes
     * @return
     */
    @PostMapping("addAthletes")
    public Map addAthletes(String password1, @RequestBody Athletes athletes) {
        Map map = new HashMap();
        if (!password1.equals(athletes.getPassword())) {
            map.put("message", "not-same");
            return map;
        }
        athletesService.save(athletes);
        map.put("message", "success");
        return map;
    }

    /**
     * 删除运动员
     *
     * @param athletesId
     * @return
     */
    @DeleteMapping("deleteAthletes/{athletesId}")
    public Map deleteAthletes(@PathVariable("athletesId") Integer athletesId) {
        Map map = new HashMap();
        boolean removeById = athletesService.removeById(athletesId);
        if (!removeById) {
            map.put("message", "nothing");
            return map;
        }
        map.put("message", "success");
        return map;
    }

    @Autowired
    private AdminService adminService;
    @Autowired
    private JudgeService judgeService;

    /**
     * 修改运动员
     *
     * @param athletes
     * @return
     */
    @PutMapping("updateAthletes")
    public Map updateAthletes(@RequestBody Athletes athletes) {
        Map map = new HashMap();
        boolean updateById = athletesService.updateById(athletes);
        if (!updateById) {
            map.put("message", "nothing");
            return map;
        }
        map.put("message", "success");
        return map;
    }
    @PutMapping("updateAdmin")
    public Map updateAdmin(@RequestBody Admin admin) {
        Map map = new HashMap();
        boolean updateById = adminService.updateById(admin);
        if (!updateById) {
            map.put("message", "nothing");
            return map;
        }
        map.put("message", "success");
        return map;
    }
    @PutMapping("updateJudge")
    public Map updateJudge(@RequestBody Judge judge) {
        Map map = new HashMap();
        boolean updateById = judgeService.updateById(judge);
        if (!updateById) {
            map.put("message", "nothing");
            return map;
        }
        map.put("message", "success");
        return map;
    }

    /**
     * 通过运动员Id查找运动员
     *
     * @param athletesId
     * @return
     */
    @GetMapping("getAthletes/{athletesId}/{role}")
    public Map getAthletes(@PathVariable("athletesId") Integer athletesId,@PathVariable("role")Integer role) {
        Map map = new HashMap();
        if (role == 0) {
            Admin admin = adminService.getById(athletesId);
            if (admin == null) {
                map.put("message", "nothing");
                return map;
            }
            map.put("message", "success");
            map.put("admin", admin);
            return map;
        } else if (role == 1) {
            Judge judge = judgeService.getById(athletesId);
            if (judge == null) {
                map.put("message", "nothing");
                return map;
            }
            map.put("message", "success");
            map.put("judge", judge);
            return map;
        } else if (role == 2) {
            Athletes athletes = athletesService.getById(athletesId);
            if (athletes == null) {
                map.put("message", "nothing");
                return map;
            }
            map.put("message", "success");
            map.put("athletes", athletes);
            return map;
        } else {
            return null;
        }
    }

    /**
     * 获取所有运动员
     *
     * @return
     */
    @GetMapping("getAllAthletes/{pageNum}/{pageSize}")
    public Map getAllAthletes(
            @RequestParam(value = "name", required = false) String name,
            @PathVariable(value = "pageNum") int pageNum,
            @PathVariable(value = "pageSize") int pageSize) {
        Map map = new HashMap();
        LambdaQueryWrapper<Athletes> wrapper = new LambdaQueryWrapper<Athletes>();
        if (!ObjectUtils.isEmpty(name)) {
            wrapper.like(Athletes::getAthletesName, name);
        }
        Page<Athletes> page = new Page<>(pageNum, pageSize);
        IPage<Athletes> page1 = athletesService.page(page, wrapper);
        List<Athletes> list = page1.getRecords();
        long page1Size = page1.getSize();
        long current = page1.getCurrent();
        long total = page1.getTotal();
        map.put("message", "success");
        map.put("list", list);
        map.put("pageSize", page1Size);
        map.put("pageNum", current);
        map.put("total", total);
        return map;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteAthletes/")
    public Map deleteAthletes(@RequestBody int[] ids) {
        boolean flags = athletesService.removeByIds(Arrays.asList(ids));
        Map map = new HashMap();
        if (!flags) {
            map.put("message", "failed");
            return map;
        }
        map.put("message", "success");
        return map;
    }
}